﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ajax_Learning.Models;
using PagedList;

namespace Ajax_Learning.Controllers
{
    public class RestaurentController : Controller
    {
        const int PAGE_SIZE = 10;
        private RestaurentsContext db = new RestaurentsContext();

        //
        // GET: /Restaurent/

        public ActionResult Index(string s, string currentFilter, int? page)
        {
            var restaurents = from r in db.Restaurents
                              select r;
            if (!String.IsNullOrEmpty(s))
            {
                page = 1;
            }
            else
            {
                s = currentFilter;
            }
            ViewBag.CurrentFilter = s;
            if (!String.IsNullOrEmpty(s))
            {
                restaurents = restaurents.Where(r => r.Name.ToUpper().Contains(s.ToUpper()));
            }
            restaurents = restaurents.OrderBy(r => r.RestaurentID);
            int pageNumber = page ?? 1;
            if (Request.IsAjaxRequest())
            {
                return PartialView("_Restaurents", restaurents.ToPagedList(pageNumber, PAGE_SIZE));
            }
            return View(restaurents.ToPagedList(pageNumber, PAGE_SIZE));
        }

        // AJAX in js file
        public ActionResult AjaxInJsFile(string s)
        {
            var restaurents = from r in db.Restaurents
                              select r;
            if (!String.IsNullOrEmpty(s))
            {
                restaurents = restaurents.Where(r => r.Name.ToUpper().Contains(s.ToUpper()));
            }
            if (Request.IsAjaxRequest())
            {
                return PartialView("_AjaxInJsFileRestaurents", restaurents.ToList());
            }
            return View(restaurents.ToList());
        }
        
        // Search auto completed with ajax
        public ActionResult AutoComplete(string term)
        {
            var restaurents = (from r in db.Restaurents
                              where r.Name.StartsWith(term)
                              orderby r.Name ascending
                              select (new {
                                  label = r.Name
                              })).Take(10);
            return Json(restaurents, JsonRequestBehavior.AllowGet);
        }

        public ActionResult AjaxAutoCompleteSearch(string s)
        {
            var restaurents = from r in db.Restaurents
                              select r;
            if (!String.IsNullOrEmpty(s))
            {
                restaurents = restaurents.Where(r => r.Name.ToUpper().Contains(s.ToUpper()));
            }
            if (Request.IsAjaxRequest())
            {
                return PartialView("_AjaxInJsFileRestaurents", restaurents.ToList());
            }
            return View(restaurents.ToList());
        }

        //
        // GET: /Restaurent/Details/5

        public ActionResult Details(int id = 0)
        {
            Restaurent restaurent = db.Restaurents.Find(id);
            if (restaurent == null)
            {
                return HttpNotFound();
            }
            return View(restaurent);
        }

        //
        // GET: /Restaurent/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Restaurent/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Restaurent restaurent)
        {
            if (ModelState.IsValid)
            {
                db.Restaurents.Add(restaurent);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(restaurent);
        }

        //
        // GET: /Restaurent/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Restaurent restaurent = db.Restaurents.Find(id);
            if (restaurent == null)
            {
                return HttpNotFound();
            }
            return View(restaurent);
        }

        //
        // POST: /Restaurent/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Restaurent restaurent)
        {
            if (ModelState.IsValid)
            {
                db.Entry(restaurent).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(restaurent);
        }

        //
        // GET: /Restaurent/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Restaurent restaurent = db.Restaurents.Find(id);
            if (restaurent == null)
            {
                return HttpNotFound();
            }
            return View(restaurent);
        }

        //
        // POST: /Restaurent/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Restaurent restaurent = db.Restaurents.Find(id);
            db.Restaurents.Remove(restaurent);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}